package com.zmm.book.keep.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * description:
 * name RestTemplateConfig
 * @author 900045
 * date By 2021-11-03 17:26:34
 */
@Configuration
public class RestTemplateConfig {


	@Bean
	@LoadBalanced
	public RestTemplate loadRestTemplate(){
		return  new RestTemplate();
	}
	
	
	/**

	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate(httpRequestFactory());
	}

	public ClientHttpRequestFactory httpRequestFactory() {
		return new HttpComponentsClientHttpRequestFactory(httpClient());
	}

	public HttpClient httpClient() {
		Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
				.register("http", PlainConnectionSocketFactory.getSocketFactory())
				.register("https", SSLConnectionSocketFactory.getSocketFactory())
				.build();
		PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
		//设置整个连接池最大连接数 根据自己的场景决定
		connectionManager.setMaxTotal(100);
		//路由是对maxTotal的细分
		connectionManager.setDefaultMaxPerRoute(100);
		RequestConfig requestConfig = RequestConfig.custom()
				//服务器返回数据(response)的时间，超过该时间抛出read timeout
				.setSocketTimeout(10000)
				//连接上服务器(握手成功)的时间，超出该时间抛出connect timeout
				.setConnectTimeout(5000)
				//从连接池中获取连接的超时时间，超过该时间未拿到可用连接，
				// 会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool
				.setConnectionRequestTimeout(1000)
				.build();
		return HttpClientBuilder.create()
				.setDefaultRequestConfig(requestConfig)
				.setConnectionManager(connectionManager)
				.build();
	}
	*/

	
}
